namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Text;
    using System.Text.RegularExpressions;

    public class Common
    {
        private static int number = 0;
        public static string Version = string.Empty;

        public static Font AddUnderLineToFont(Font oFont)
        {
            FontStyle regular = FontStyle.Regular;
            if ((oFont.Bold && oFont.Italic) && oFont.Strikeout)
            {
                regular = FontStyle.Strikeout | FontStyle.Underline | FontStyle.Italic | FontStyle.Bold;
            }
            else if (oFont.Bold && oFont.Italic)
            {
                regular = FontStyle.Underline | FontStyle.Italic | FontStyle.Bold;
            }
            else if (oFont.Italic && oFont.Strikeout)
            {
                regular = FontStyle.Strikeout | FontStyle.Underline | FontStyle.Italic;
            }
            else if (oFont.Bold && oFont.Strikeout)
            {
                regular = FontStyle.Strikeout | FontStyle.Underline | FontStyle.Bold;
            }
            else if (oFont.Bold)
            {
                regular = FontStyle.Underline | FontStyle.Bold;
            }
            else if (oFont.Italic)
            {
                regular = FontStyle.Underline | FontStyle.Italic;
            }
            else if (oFont.Strikeout)
            {
                regular = FontStyle.Strikeout | FontStyle.Underline;
            }
            else
            {
                regular = FontStyle.Underline;
            }
            return new Font(oFont.FontFamily, oFont.Size, regular);
        }

        public static double CalculateAvedev(string target, int cycle, double curValue, double maValue, DataTable dataSource, int r)
        {
            if (!dataSource.Columns.Contains(target))
            {
                return 0.0;
            }
            int num = r - (cycle - 1);
            if (num < 0)
            {
                num = 0;
            }
            double num2 = Math.Abs((double) (curValue - maValue));
            for (int i = r - 1; i >= num; i--)
            {
                double num4 = Convert.ToDouble(dataSource.Rows[i][target]);
                num2 += Math.Abs((double) (num4 - maValue));
            }
            int num5 = cycle;
            if (r <= (cycle - 1))
            {
                num5 = r + 1;
            }
            return (num2 / ((double) num5));
        }

        public static double CalculateExponentialMovingAvg(string field, string target, int cycle, double curValue, DataTable dataSource, int r)
        {
            if (!dataSource.Columns.Contains(field) || !dataSource.Columns.Contains(target))
            {
                return 0.0;
            }
            DataRow row1 = dataSource.Rows[r];
            double num2 = 0.0;
            if (r > 0)
            {
                num2 = Convert.ToDouble(dataSource.Rows[r - 1][field]);
            }
            int num3 = cycle;
            if (r <= (cycle - 1))
            {
                num3 = r + 1;
            }
            return (((curValue * 2.0) + (num2 * (num3 - 1))) / ((double) (num3 + 1)));
        }

        public static double CalculateRawStochasticValue(int r, int kPeriods, string close, string high, string low, DataTable dataSource)
        {
            int num = r - (kPeriods - 1);
            if (num < 0)
            {
                num = 0;
            }
            double num2 = Convert.ToDouble(dataSource.Rows[r][close]);
            List<double> valueList = new List<double>();
            List<double> list2 = new List<double>();
            for (int i = r; i >= num; i--)
            {
                double item = Convert.ToDouble(dataSource.Rows[i][high]);
                double num5 = Convert.ToDouble(dataSource.Rows[i][low]);
                valueList.Add(item);
                list2.Add(num5);
            }
            double highValue = GetHighValue(valueList);
            double lowValue = GetLowValue(list2);
            return (((num2 - lowValue) / (highValue - lowValue)) * 100.0);
        }

        public static double CalculateStandardDeviation(string target, int cycle, double standardDeviation, double targetValue, double agvValue, DataTable dataSource, int r)
        {
            if (!dataSource.Columns.Contains(target))
            {
                return 0.0;
            }
            double num = (targetValue - agvValue) * (targetValue - agvValue);
            int num2 = r - (cycle - 1);
            if (num2 < 0)
            {
                num2 = 0;
            }
            for (int i = r - 1; i >= num2; i--)
            {
                double num4 = Convert.ToDouble(dataSource.Rows[i][target]);
                num += (num4 - agvValue) * (num4 - agvValue);
            }
            int num5 = cycle;
            if (r <= (cycle - 1))
            {
                num5 = r + 1;
            }
            return (standardDeviation * Math.Sqrt(num / ((double) num5)));
        }

        public static double CalculateTrueRange(int r, double curHigh, double curLow, string high, string low, DataTable dataSource)
        {
            double num = 0.0;
            double num2 = 0.0;
            if (r > 0)
            {
                num = Convert.ToDouble(dataSource.Rows[r - 1][high]);
                num2 = Convert.ToDouble(dataSource.Rows[r - 1][low]);
            }
            if (Math.Abs((double) (curHigh - num)) < Math.Abs((double) (curLow - num2)))
            {
                return Math.Abs((double) (curLow - num2));
            }
            return Math.Abs((double) (curHigh - num));
        }

        public static double CalcutaSumilationValue(int r, string field, int cycle, string target, double curValue, DataTable dataSource)
        {
            double num = 0.0;
            int num2 = r - (cycle - 1);
            if (num2 < 0)
            {
                num2 = 0;
            }
            if (r <= (cycle - 1))
            {
                for (int i = r - 1; i >= num2; i--)
                {
                    num += Convert.ToDouble(dataSource.Rows[i][target]);
                }
            }
            else if (r > (cycle - 1))
            {
                num = Convert.ToDouble(dataSource.Rows[r - 1][field]) - Convert.ToDouble(dataSource.Rows[r - cycle][target]);
            }
            return (num + curValue);
        }

        public static double CalcuteMovingAvg(int r, int cycle, string field, string target, double curValue, DataTable dataSource)
        {
            if (!dataSource.Columns.Contains(field) || !dataSource.Columns.Contains(target))
            {
                return 0.0;
            }
            double num = 0.0;
            int num2 = r - (cycle - 1);
            if (num2 < 0)
            {
                num2 = 0;
            }
            int num3 = cycle;
            if (r <= (cycle - 1))
            {
                for (int i = r - 1; i >= num2; i--)
                {
                    num += Convert.ToDouble(dataSource.Rows[i][target]);
                }
                num3 = r + 1;
            }
            else if (r > (cycle - 1))
            {
                num = Convert.ToDouble(dataSource.Rows[r - 1][field]) * cycle;
                num -= Convert.ToDouble(dataSource.Rows[r - cycle][target]);
            }
            num += curValue;
            return (num / ((double) num3));
        }

        public static double CalcuteSimpleMovingAvg(int r, int cycle, int weight, string field, string target, double curValue, DataTable dataSource)
        {
            if (!dataSource.Columns.Contains(field) || !dataSource.Columns.Contains(target))
            {
                return 0.0;
            }
            int num = cycle;
            if (r <= (cycle - 1))
            {
                num = r + 1;
            }
            double num2 = 0.0;
            if (r > 0)
            {
                num2 = Convert.ToDouble(dataSource.Rows[r - 1][field]);
            }
            return (((curValue * weight) + ((num - weight) * num2)) / ((double) num));
        }

        public static string GetCalenderFormatTimeKey(string value, string format)
        {
            string replacement = (value.Length >= 4) ? value.Substring(0, 4) : "";
            string str3 = (value.Length >= 6) ? value.Substring(4, 2) : "";
            string str4 = (value.Length >= 8) ? value.Substring(6, 2) : "";
            string str5 = (value.Length >= 10) ? value.Substring(8, 2) : "";
            string str6 = (value.Length >= 12) ? value.Substring(10, 2) : "";
            string str7 = (value.Length >= 14) ? value.Substring(12, 2) : "";
            return Regex.Replace(Regex.Replace(Regex.Replace(Regex.Replace(Regex.Replace(Regex.Replace(format, "yyyy", replacement), "MM", str3), "dd", str4), "hh", str5), "mm", str6), "ss", str7);
        }

        public static Color GetColorByHtmlHex(string color)
        {
            return ColorTranslator.FromHtml(color);
        }

        public static DateTime GetDateTimeByTimeKey(string timeKey)
        {
            int year = (timeKey.Length >= 4) ? Convert.ToInt32(timeKey.Substring(0, 4)) : 0x7b2;
            int month = (timeKey.Length >= 6) ? Convert.ToInt32(timeKey.Substring(4, 2)) : 1;
            int day = (timeKey.Length >= 8) ? Convert.ToInt32(timeKey.Substring(6, 2)) : 1;
            int hour = (timeKey.Length >= 10) ? Convert.ToInt32(timeKey.Substring(8, 2)) : 0;
            int minute = (timeKey.Length >= 12) ? Convert.ToInt32(timeKey.Substring(10, 2)) : 0;
            return new DateTime(year, month, day, hour, minute, (timeKey.Length >= 14) ? Convert.ToInt32(timeKey.Substring(12, 2)) : 0);
        }

        public static int GetFibonacciValue(int index)
        {
            if (index < 1)
            {
                return 0;
            }
            List<int> list = new List<int>();
            for (int i = 0; i <= (index - 1); i++)
            {
                switch (i)
                {
                    case 0:
                    case 1:
                        list.Add(1);
                        break;

                    default:
                        list.Add(list[i - 1] + list[i - 2]);
                        break;
                }
            }
            return list[index - 1];
        }

        public static string GetGuid()
        {
            return Guid.NewGuid().ToString();
        }

        public static int GetHighRecord(List<object[]> dicValues)
        {
            double num = 0.0;
            int num2 = -1;
            for (int i = 0; i < dicValues.Count; i++)
            {
                int num4 = Convert.ToInt32(dicValues[i][0]);
                double num5 = Convert.ToDouble(dicValues[i][1]);
                if (i == 0)
                {
                    num2 = num4;
                    num = num5;
                }
                else if (num < num5)
                {
                    num = num5;
                    num2 = num4;
                }
            }
            return num2;
        }

        public static double GetHighValue(List<double> valueList)
        {
            double num = 0.0;
            for (int i = 0; i < valueList.Count; i++)
            {
                if (i == 0)
                {
                    num = valueList[i];
                }
                else if (num < valueList[i])
                {
                    num = valueList[i];
                }
            }
            return num;
        }

        public static double[] GetLineParam(float x1, float y1, float x2, float y2)
        {
            double num = 0.0;
            if ((x2 - x1) != 0f)
            {
                num = (y2 - y1) / (x2 - x1);
                double num2 = y1 - (num * x1);
                return new double[] { num, num2 };
            }
            return null;
        }

        public static int GetLoweRecord(List<object[]> dicValues)
        {
            double num = 0.0;
            int num2 = -1;
            for (int i = 0; i < dicValues.Count; i++)
            {
                int num4 = Convert.ToInt32(dicValues[i][0]);
                double num5 = Convert.ToDouble(dicValues[i][1]);
                if (i == 0)
                {
                    num2 = num4;
                    num = num5;
                }
                else if (num > num5)
                {
                    num2 = num4;
                    num = num5;
                }
            }
            return num2;
        }

        public static double GetLowValue(List<double> valueList)
        {
            double num = 0.0;
            for (int i = 0; i < valueList.Count; i++)
            {
                if (i == 0)
                {
                    num = valueList[i];
                }
                else if (num > valueList[i])
                {
                    num = valueList[i];
                }
            }
            return num;
        }

        public static int GetNewNumber()
        {
            return ++number;
        }

        public static Color GetReverseColor(Color originalColor)
        {
            return Color.FromArgb(0xff - originalColor.R, 0xff - originalColor.G, 0xff - originalColor.B);
        }

        public static GraphicsPath GetRoundRectangle(int cornerRadius, RectangleF rect)
        {
            GraphicsPath path = new GraphicsPath();
            path.AddArc(rect.X, rect.Y, (float) (cornerRadius * 2), (float) (cornerRadius * 2), 180f, 90f);
            path.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - (cornerRadius * 2), rect.Y);
            path.AddArc((rect.X + rect.Width) - (cornerRadius * 2), rect.Y, (float) (cornerRadius * 2), (float) (cornerRadius * 2), 270f, 90f);
            path.AddLine(rect.Right, rect.Y + (cornerRadius * 2), rect.Right, (rect.Y + rect.Height) - (cornerRadius * 2));
            path.AddArc((rect.X + rect.Width) - (cornerRadius * 2), (rect.Y + rect.Height) - (cornerRadius * 2), (float) (cornerRadius * 2), (float) (cornerRadius * 2), 0f, 90f);
            path.AddLine(rect.Right - (cornerRadius * 2), rect.Bottom, rect.X + (cornerRadius * 2), rect.Bottom);
            path.AddArc(rect.X, rect.Bottom - (cornerRadius * 2), (float) (cornerRadius * 2), (float) (cornerRadius * 2), 90f, 90f);
            path.AddLine(rect.X, rect.Bottom - (cornerRadius * 2), rect.X, rect.Y + (cornerRadius * 2));
            path.CloseFigure();
            return path;
        }

        public static RectangleF GetSignaleMarkScope(float x, float y, float width, MarkStyle markStyle)
        {
            RectangleF ef = new RectangleF();
            if (width > 10f)
            {
                width = 14f;
            }
            switch (markStyle)
            {
                case MarkStyle.UpArrow:
                    return new RectangleF(x - (width / 2f), y, width, (width * 3f) / 2f);

                case MarkStyle.DownArrow:
                    return new RectangleF(x - (width / 2f), y - ((width * 3f) / 2f), width, (width * 3f) / 2f);

                case MarkStyle.UpArrowWithOutTail:
                    return new RectangleF(x - (width / 2f), y, width, width);

                case MarkStyle.DownArrowWithOutTail:
                    return new RectangleF(x - (width / 2f), y - width, width, width);

                case MarkStyle.LeftArrow:
                case MarkStyle.RightArrow:
                    return new RectangleF(x - (width / 2f), y - (width / 2f), width, width);

                case MarkStyle.Cycle:
                    return new RectangleF(x - (width / 3f), y - (width / 3f), (width * 2f) / 3f, (width * 2f) / 3f);
            }
            return ef;
        }

        public static GraphicsPath GetSignalMarkShape(float x, float y, float width, MarkStyle markStyle)
        {
            if (width > 10f)
            {
                width = 14f;
            }
            GraphicsPath path = new GraphicsPath();
            switch (markStyle)
            {
                case MarkStyle.UpArrow:
                    path.AddLine(x, y, x + (width / 2f), y + width);
                    path.AddLine((float) (x + (width / 2f)), (float) (y + width), (float) (x + (width / 4f)), (float) (y + width));
                    path.AddLine((float) (x + (width / 4f)), (float) (y + width), (float) (x + (width / 4f)), (float) (y + ((width * 3f) / 2f)));
                    path.AddLine((float) (x + (width / 4f)), (float) (y + ((width * 3f) / 2f)), (float) (x - (width / 4f)), (float) (y + ((width * 3f) / 2f)));
                    path.AddLine((float) (x - (width / 4f)), (float) (y + ((width * 3f) / 2f)), (float) (x - (width / 4f)), (float) (y + width));
                    path.AddLine((float) (x - (width / 4f)), (float) (y + width), (float) (x - (width / 2f)), (float) (y + width));
                    path.AddLine(x - (width / 2f), y + width, x, y);
                    break;

                case MarkStyle.DownArrow:
                    path.AddLine(x, y, x + (width / 2f), y - width);
                    path.AddLine((float) (x + (width / 2f)), (float) (y - width), (float) (x + (width / 4f)), (float) (y - width));
                    path.AddLine((float) (x + (width / 4f)), (float) (y - width), (float) (x + (width / 4f)), (float) (y - ((width * 3f) / 2f)));
                    path.AddLine((float) (x + (width / 4f)), (float) (y - ((width * 3f) / 2f)), (float) (x - (width / 4f)), (float) (y - ((width * 3f) / 2f)));
                    path.AddLine((float) (x - (width / 4f)), (float) (y - ((width * 3f) / 2f)), (float) (x - (width / 4f)), (float) (y - width));
                    path.AddLine((float) (x - (width / 4f)), (float) (y - width), (float) (x - (width / 2f)), (float) (y - width));
                    path.AddLine(x - (width / 2f), y - width, x, y);
                    break;

                case MarkStyle.UpArrowWithOutTail:
                    path.AddLine(x, y, x + (width / 2f), y + width);
                    path.AddLine((float) (x + (width / 2f)), (float) (y + width), (float) (x - (width / 2f)), (float) (y + width));
                    path.AddLine(x - (width / 2f), y + width, x, y);
                    break;

                case MarkStyle.DownArrowWithOutTail:
                    path.AddLine(x, y, x + (width / 2f), y - width);
                    path.AddLine((float) (x + (width / 2f)), (float) (y - width), (float) (x - (width / 2f)), (float) (y - width));
                    path.AddLine(x - (width / 2f), y - width, x, y);
                    break;

                case MarkStyle.LeftArrow:
                    path.AddLine(x + (width / 2f), y, x - (width / 2f), y - (width / 2f));
                    path.AddLine((float) (x - (width / 2f)), (float) (y - (width / 2f)), (float) (x - (width / 2f)), (float) (y + (width / 2f)));
                    path.AddLine(x - (width / 2f), y + (width / 2f), x + (width / 2f), y);
                    break;

                case MarkStyle.RightArrow:
                    path.AddLine(x - (width / 2f), y, x + (width / 2f), y - (width / 2f));
                    path.AddLine((float) (x + (width / 2f)), (float) (y - (width / 2f)), (float) (x + (width / 2f)), (float) (y + (width / 2f)));
                    path.AddLine(x + (width / 2f), y + (width / 2f), x - (width / 2f), y);
                    break;

                case MarkStyle.Cycle:
                    path.AddEllipse((float) (x - (width / 3f)), (float) (y - (width / 3f)), (float) ((width * 2f) / 3f), (float) ((width * 2f) / 3f));
                    break;
            }
            path.CloseFigure();
            return path;
        }

        public static string GetTimeKey(DateTime dt)
        {
            string str = (dt.Month.ToString().Length == 1) ? ("0" + dt.Month.ToString()) : dt.Month.ToString();
            string str2 = (dt.Day.ToString().Length == 1) ? ("0" + dt.Day.ToString()) : dt.Day.ToString();
            string str3 = (dt.Hour.ToString().Length == 1) ? ("0" + dt.Hour.ToString()) : dt.Hour.ToString();
            string str4 = (dt.Minute.ToString().Length == 1) ? ("0" + dt.Minute.ToString()) : dt.Minute.ToString();
            string str5 = (dt.Second.ToString().Length == 1) ? ("0" + dt.Second.ToString()) : dt.Second.ToString();
            return string.Concat(new object[] { dt.Year, str, str2, str3, str4, str5 });
        }

        public static string GetValueByDigit(double value, int digit, bool round)
        {
            if (!round)
            {
                StringBuilder builder = new StringBuilder();
                string str = value.ToString();
                if (str.IndexOf(".") != -1)
                {
                    builder.Append(str.Substring(0, str.IndexOf(".")));
                    if (digit > 0)
                    {
                        builder.Append(".");
                    }
                    for (int i = 0; i < digit; i++)
                    {
                        int startIndex = str.IndexOf(".") + (i + 1);
                        if (startIndex <= (str.Length - 1))
                        {
                            builder.Append(str.Substring(startIndex, 1));
                        }
                        else
                        {
                            builder.Append("0");
                        }
                    }
                }
                else
                {
                    builder.Append(str);
                    if (digit > 0)
                    {
                        builder.Append(".");
                    }
                    for (int j = 0; j < digit; j++)
                    {
                        builder.Append("0");
                    }
                }
                return builder.ToString();
            }
            StringBuilder builder2 = new StringBuilder();
            builder2.Append("0");
            if (digit > 0)
            {
                builder2.Append(".");
                for (int k = 0; k < digit; k++)
                {
                    builder2.Append("0");
                }
            }
            return value.ToString(builder2.ToString());
        }

        public static int GridScale(double XMin, double XMax, int N, ref double SMin, ref double Step, double m_fTick)
        {
            int num;
            double num5;
            int[] numArray = new int[] { 10, 12, 15, 0x10, 20, 0x19, 30, 40, 50, 60, 0x4b, 80, 100, 120, 150 };
            int length = numArray.Length;
            if (XMin > XMax)
            {
                num5 = XMin;
                XMin = XMax;
                XMax = num5;
            }
            if (XMin == XMax)
            {
                XMax = (XMin == 0.0) ? 1.0 : (XMin + (Math.Abs(XMin) / 10.0));
            }
            if (XMax <= 0.0)
            {
                num = 1;
                num5 = XMin;
                XMin = -XMax;
                XMax = -num5;
            }
            else
            {
                num = 0;
            }
            if (N < 2)
            {
                N = 2;
            }
            int num2 = N - 1;
            for (int i = 0; i < 3; i++)
            {
                double num3 = (XMax - XMin) / ((double) num2);
                double num4 = num3;
                int num10 = 0;
                while (num4 < 10.0)
                {
                    num10--;
                    num4 *= 10.0;
                }
                while (num4 > 100.0)
                {
                    num10++;
                    num4 /= 10.0;
                }
                int index = 0;
                while ((index < length) && (num4 > numArray[index]))
                {
                    index++;
                }
                do
                {
                    Step = numArray[index] * Math.Pow(10.0, (double) num10);
                    if (m_fTick != 0.0)
                    {
                        Step = Math.Floor((double) (Step / m_fTick)) * m_fTick;
                    }
                    SMin = Math.Floor((double) (XMin / Step)) * Step;
                    double num6 = SMin + (num2 * Step);
                    if (XMax <= num6)
                    {
                        if (num == 1)
                        {
                            SMin = -num6;
                        }
                        Step *= num2 / (N - 1);
                        return 1;
                    }
                    index++;
                }
                while (index < length);
                num2 *= 2;
            }
            return 0;
        }

        public static double[] LinearRegressionEquation(List<double> vList)
        {
            if (vList.Count <= 1)
            {
                return null;
            }
            double num = 0.0;
            double num2 = 0.0;
            for (int i = 0; i < vList.Count; i++)
            {
                num += i + 1;
                num2 += vList[i];
            }
            double num4 = num / ((double) vList.Count);
            double num5 = num2 / ((double) vList.Count);
            double num6 = 0.0;
            double num7 = 0.0;
            for (int j = 0; j < vList.Count; j++)
            {
                num6 += ((j + 1) - num4) * (vList[j] - num5);
                num7 += ((j + 1) - num4) * ((j + 1) - num4);
            }
            double num9 = num6 / num7;
            double num10 = num5 - (num9 * num4);
            return new double[] { num9, num10 };
        }
    }
}

